﻿using MongoDB.Driver;
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Web.Mvc;

namespace bonus.Controllers
{
	public class ExcelController : Controller
	{
		/// <summary>
		/// 导出公司/服务中心数据数据
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public JsonResult ExportExecl(int type)
		{

			try
			{
				//设置导出文件路径
				string path = "c:\\";

				//设置新建文件路径及名称
				string savePath = path + Models.User.TypeClass.GetType(type) + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xls";

				//创建文件
				FileStream file = new FileStream(savePath, FileMode.CreateNew, FileAccess.Write);

				//以指定的字符编码向指定的流写入字符
				StreamWriter sw = new StreamWriter(file, System.Text.Encoding.GetEncoding("GB2312"));

				StringBuilder strbu = new StringBuilder();

				Repository.Repository tool = new Repository.Repository();

				var filter = Builders<Models.User>.Filter.Where(p => p.UserType == type);

				var infos = tool.GetMany(filter);

				System.Data.DataTable dt = new System.Data.DataTable();
				dt.Columns.Add("序号");
				dt.Columns.Add("编号");
				dt.Columns.Add("姓名");
				dt.Columns.Add("手机号");
				int ii = 1;
				foreach (var info in infos)
				{
					DataRow dr = dt.NewRow();
					dr["序号"] = ii;
					dr["编号"] = info.NO;
					dr["姓名"] = info.Name;
					dr["手机号"] = string.IsNullOrEmpty(info.Mobile) ? "无" : info.Mobile;
					dt.Rows.Add(dr);
					ii += 1;
				}
				//写入标题
				for (int i = 0; i < dt.Columns.Count; i++)
				{
					strbu.Append(dt.Columns[i].ColumnName.ToString() + "\t");
				}
				//加入换行字符串
				strbu.Append(Environment.NewLine);

				//写入内容
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					for (int j = 0; j < dt.Columns.Count; j++)
					{
						strbu.Append(dt.Rows[i][j].ToString() + "\t");
					}
					strbu.Append(Environment.NewLine);
				}

				sw.Write(strbu.ToString());
				sw.Flush();
				file.Flush();

				sw.Close();
				sw.Dispose();

				file.Close();
				file.Dispose();

				return Json(new { Code = "OK", Msg = "数据已保存在:" + savePath });
			}
			catch (Exception e)
			{
				return Json(new { Code = "OK", Msg = e.Message });
			}

		}




		/// <summary>
		/// 导出规定时间内订单报表数据数据
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public JsonResult ExportOrder(DateTime starteDT, DateTime endDT)
		{
			//设置导出文件路径
			string path = "c:\\";

			//设置新建文件路径及名称
			string savePath = path + "报单记录" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xls";

			//创建文件
			FileStream file = new FileStream(savePath, FileMode.CreateNew, FileAccess.Write);

			//以指定的字符编码向指定的流写入字符
			StreamWriter sw = new StreamWriter(file, System.Text.Encoding.GetEncoding("GB2312"));

			StringBuilder strbu = new StringBuilder();

			Repository.Repository tool = new Repository.Repository();

			var filter = Builders<Models.Order>.Filter.Where(p => p.AddDT >= starteDT && p.AddDT <= endDT);

			var infos = tool.GetMany(filter);

			System.Data.DataTable dt = new System.Data.DataTable();
			dt.Columns.Add("序号");
			dt.Columns.Add("订单编号");
			dt.Columns.Add("学员编号");
			dt.Columns.Add("学员姓名");
			dt.Columns.Add("手机号");
			dt.Columns.Add("商品总价");
			dt.Columns.Add("PV值");
			dt.Columns.Add("产品名称+数量");
			dt.Columns.Add("产品数量");
			dt.Columns.Add("创建时间");
			int ii = 1;
			foreach (var info in infos)
			{
				DataRow dr = dt.NewRow();
				dr["序号"] = ii;
				dr["订单编号"] = string.Format(info.OrderNo);
				dr["学员编号"] = string.IsNullOrEmpty(info.SaleNo) ? "无" : info.SaleNo;//学员编号
				dr["学员姓名"] = string.IsNullOrEmpty(info.UserName) ? "无" : info.UserName;//学员姓名
				dr["手机号"] = string.IsNullOrEmpty(info.Mobile) ? "无" : info.Mobile;
				dr["商品总价"] = string.Format(info.Money.ToString("0.##"));//学员编号
				dr["PV值"] = info.PV;//学员编号
				string str = "";
				int num = 0;
				foreach (var item in info.Products)
				{
					num += item.Num;
					str += item.Name + "+" + item.Num + ";";
				}
				dr["产品名称+数量"] = str;//学员编号
				dr["产品数量"] = num;//学员编号
				dr["创建时间"] = string.Format(info.AddDT.ToString("yyyy年MM月dd日"));
				dt.Rows.Add(dr);
				ii += 1;
			}
			//写入标题
			for (int i = 0; i < dt.Columns.Count; i++)
			{
				strbu.Append(dt.Columns[i].ColumnName.ToString() + "\t");
			}
			//加入换行字符串
			strbu.Append(Environment.NewLine);

			//写入内容
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				for (int j = 0; j < dt.Columns.Count; j++)
				{
					strbu.Append(dt.Rows[i][j].ToString() + "\t");
				}
				strbu.Append(Environment.NewLine);
			}

			sw.Write(strbu.ToString());
			sw.Flush();
			file.Flush();

			sw.Close();
			sw.Dispose();

			file.Close();
			file.Dispose();

			return Json(new { Code = "OK", Msg = "数据以保存在:" + savePath });
		}

	}
}